#define _CRT_SECURE_NO_WARNINGS




#include<iostream>
using namespace std;
const int N = 15;
int a[N];
int t;
int main()
{
    cin >> t;
    while (t--)
    {
        int cnt = 0;
        for (int i = 1; i <= 12; i++)
        {
            cin >> a[i];
        }
        int l = 1, r = 12;
        while (l < r)
        {
            while (a[l] == 0 && l < r)
                l++;
            while (a[r] == 0 && l < r)
                r--;
            if (l >= r)
                break;
            // cout << l << " " << r << endl;
            if (l * r <= 12)
            {
                if (a[l] < a[r])
                {
                    a[r] -= a[l];
                    cnt += a[l];
                    a[l] = 0;
                    l++;
                }
                else if (a[l] > a[r])
                {
                    a[l] -= a[r];
                    cnt += a[r];
                    a[r] = 0;
                    r--;

                }
                else
                {
                    cnt += a[r];
                    a[r] = a[l] = 0;
                    l++, r--;

                }
            }
            else
            {
                cnt += a[r];
                a[r] = 0;
                r--;
            }
        }
        cout << cnt << "\n";
    }
    return 0;
}
